package com.lss.myapp.activity;

import static com.lss.myapp.constant.AppConfig.SP_USER;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public abstract class BaseActivity extends AppCompatActivity {
    public Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

    }


    // 封装存储方法 ----------------------------------------------------------------
    protected void saveUserToken(String token, int expire) {
        SharedPreferences.Editor editor = getSharedPreferences(SP_USER, MODE_PRIVATE).edit();
        editor.putString("token", token);
        editor.putInt("expire", expire);
        editor.apply();
    }

    // 封装读取方法（按需添加）-----------------------------------------------------
    protected String getToken() {
        return getSharedPreferences(SP_USER, MODE_PRIVATE).getString("token", "");
    }

    protected int getExpire() {
        return getSharedPreferences(SP_USER, MODE_PRIVATE).getInt("expire", 0);
    }

    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    public void navigateTo(Class cls) {
        Intent in = new Intent(mContext, cls);
        startActivity(in);
    }


}
